package com.youdianstar.app.common.util

import android.text.TextUtils
import com.youdianstar.app.common.entity.ConfigItem
import com.youdianstar.app.common.entity.UserInfo
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.tencent.mmkv.MMKV


/**
 * File: CacheUtil
 * Author: 82149 Create: 2021/9/25 10:29
 * Changes (from 2021/9/25)
 * --------------------------------------------------
 * description: 基于tencent的mmkv 处理键值对数据,替换SharedPreferences
 * ---------------------------------------------------
 *
 */
class CacheUtil {
    companion object {
        val instance by lazy { CacheUtil() }
    }

    /**
     * 获取存储配置信息
     */
    fun getSystemConfig(): List<ConfigItem>? {
        val kv = MMKV.mmkvWithID("app")
        val listStr = kv.decodeString("system")
        return if (TextUtils.isEmpty(listStr)) {
            null
        } else {
            Gson().fromJson<List<ConfigItem>>(listStr, object : TypeToken<List<ConfigItem>>() {}.type)
        }
    }

    /**
     * 存储配置信息
     */
    fun setSystemConfig(configs: List<ConfigItem>) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("system", Gson().toJson(configs))
    }

    /**
     * 获取保存的账户信息
     */
    fun getUser(): UserInfo? {
        val kv = MMKV.mmkvWithID("app")
        val userStr = kv.decodeString("user")
        return if (TextUtils.isEmpty(userStr)) {
            null
        } else {
            Gson().fromJson(userStr, UserInfo::class.java)
        }
    }

    /**
     * 设置账户信息
     */
    fun setUser(userResponse: UserInfo?) {
        val kv = MMKV.mmkvWithID("app")
        if (userResponse == null) {
            kv.encode("user", "")
            setLoginToken("")
        } else {
            kv.encode("user", Gson().toJson(userResponse))
            userResponse.token?.let {
                setLoginToken(it)
            }
        }
    }

    /**
     * 是否已经登录
     */
    fun isLogin(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        val token = kv.decodeString("token")
        //Log.i("Token","---getCache:"+token)
        return !StrUtil.isEmpty(token)
    }

    fun getLoginToken() :String?{
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString("token")
    }


    /**
     * 设置是否已经登录
     */
    fun setLoginToken(token: String) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("token", token)
    }

    /**
     * 是否是第一次登陆
     */
    fun isFirst(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("first", true)
    }

    /**
     * 是否是第一次登陆
     */
    fun setFirst(first:Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("first", first)
    }

    /**获取保存的数值*/
    fun getLongValue(key:String): Long{
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeLong(key,0L)
    }

    /**获取保存的数值*/
    fun setLongValue(key:String,value:Long){
        val kv = MMKV.mmkvWithID("app")
        kv.encode(key,value)
    }
    /**获取保存的数值*/
    fun getIntValue(key:String): Int{
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeInt(key,0)
    }

    /**获取保存的数值*/
    fun setIntValue(key:String,value:Int){
        val kv = MMKV.mmkvWithID("app")
        kv.encode(key,value)
    }

    /**获取保存的数值*/
    fun getStringValue(key:String,defaultValue:String): String{
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString(key,defaultValue)?:""
    }

    /**获取保存的数值*/
    fun setStringValue(key:String,value:String){
        val kv = MMKV.mmkvWithID("app")
        kv.encode(key,value)
    }
}